<?php
/**
 * User create
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo prüfen ob bereits ein user mit dem namen existiert
 */
class User_CreateController extends Zend_Controller_Action
{
    /**
     * Initialisation
     *
     * @return void
     */
    public function init()
    {

    }
    /**
     * Show create form
     *
     * @return void
     */
    public function indexAction()
    {
       $this->setCss('window');
       $this->setCss('create');

        $layout = Zend_Layout::getMvcInstance();
        $layout->appTab = User_Model_Tab::render($this->view);

        $form = new User_Form_Create;
        $buildForm = $form->build();

        $request = $this->_request->getPost();

        if($request) {

            if($buildForm->isValid($request)) {
                $this->_forward('create');
            }
        }
        $this->view->form = $buildForm;
    }
    /**
     * User create
     *
     * @throws Exception
     * @return void
     */
    public function createAction() {
        try {
            $newUser = $this->_request->getPost();

            if($newUser['password'] != $newUser['passwordRepeat']) {
                throw new Exception('Passwörter stimmen nicht überein!');
            }
            $newUser['user'] = strtolower($newUser['user']);

            if(User_Model_Data::save($newUser) === FALSE) {
                throw new Exception(
                    'Benutzer ' . $newUser['user'] .
                    ' konnte nicht gespeichert werden!'
                );
            }
            $this->view->user = $newUser['user'];

        } catch (Exception $e) {

            $this->_forward(
                'error',
                null,
                null,
                array('errorMessage' => $e->getMessage())
            );
        }
    }
    /**
     * Error output
     *
     * @return void
     */
    public function errorAction() {

        $this->setCss('error');
        $this->view->errorMessage = $this->_request->errorMessage;
    }
    protected function setCss($file) {

        $cssPath = $this->view->baseUrl('application/modules/user/public/css');
        $this->view
             ->headLink()
             ->appendStylesheet(
                 $cssPath . DIRECTORY_SEPARATOR .
                 $file . ".css"
               );
    }
}